package com.ang;

import lombok.Data;

/**
 * @author ANG.YU
 * @date 2020-09-21 10:18
 */
public class VolatileTest {
    public static void main(String[] args) {
        ThreadDemo threadDemo = new ThreadDemo();
        Thread thread = new Thread(threadDemo);
        thread.start();
        while (true) {
            if (threadDemo.isFlag()) {
                System.out.println("main读取的flag = "+threadDemo.isFlag());
                break;
            }
        }
    }
}
@Data
class ThreadDemo implements Runnable {
    public volatile boolean flag = false;
    @Override
    public void run() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag = true;
        System.out.println("修改后的flag = " + flag);
    }
}